package org.clockworkmages.games.anno1186;

import java.util.ArrayList;
import java.util.List;

import org.clockworkmages.games.anno1186.model.option.Option;
import org.clockworkmages.games.anno1186.scripting.ScriptingService;

public class OptionUtil {

	public static List<Option> getAvailableOptions(List<Option> options) {

		ScriptingService scriptingService = GameBeansContext
				.getBean(ScriptingService.class);

		List<Option> availableOptions = new ArrayList<Option>();
		boolean orGroupAlreadyEvaluated = false;
		for (Option option : options) {
			String condition = option.getCondition();
			boolean isOrOption = option.isOr();
			if (isOrOption && orGroupAlreadyEvaluated) {
				continue;
			}
			if (condition == null || scriptingService.conditionIsMet(condition)) {
				availableOptions.add(option);
				orGroupAlreadyEvaluated = true;
			} else {
				orGroupAlreadyEvaluated = false;
			}
		}
		return availableOptions;
	}
}
